﻿function MarkAsRead(noteID) {

    //alert(noteID);

    $().SPServices({
        operation: "UpdateListItems",
        listName: "Notifications",
        ID: noteID,
        valuepairs: [["ReadStatus", "Read"]],
        completefunc: function (xData, Status) {

            //alert(noteID + "Mark as Read");

        }
    });

}

function ShowNotifications() {

    // *************************************************************
    // Display notifications from 'Notifications' List
    // *************************************************************

    $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: "Notifications",
        CAMLQuery: query,
        CAMLViewFields: fields,
        completefunc: function (xData, status) {

            // jGrowl defaults settings
            $.jGrowl.defaults.position = "bottom-right";
            $.jGrowl.defaults.life = 10000;
            $.jGrowl.defaults.pool = 3;
            $.jGrowl.defaults.closer = true;

            $(xData.responseXML).find("z\\:row").each(function () {

                var noteID = $(this).attr("ows_ID");

                var messageHtml =
		                "<div class='ms-vb'>" +
		                "<a href='" + L_Menu_BaseUrl + "/Lists/Notifications/DispForm.aspx?ID=" + noteID
		                + "&Source=" + window.location + "'>" +
		                "<img src='/_layouts/images/ITTASK.GIF' border='0' align='middle'> " +
		                $(this).attr("ows_Title") + "</a>" +
		                "<br/>" + $(this).attr("ows_NotificationDesc") +
		                "</div>";

                $('#one').jGrowl(messageHtml, {
                    sticky: true,
                    //header: 'Important',
                    close: function (e, m, o) {
                        // change notification status to read/closed.
                        MarkAsRead(noteID);
                    } // end - close
                }); // end - jGrowl

            }); // end - each function

        } // end - completefunc

    }); // end - SPServices

    // *************************************************************
    // END - Display notifications from 'Notifications' List
    // *************************************************************

}

function ShowOpenTasks() {

    // *************************************************************
    // Display notifications from 'Tasks' List
    // *************************************************************

    $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: "Tasks",
        CAMLQuery: queryTasks,
        CAMLViewFields: fieldsTasks,
        completefunc: function (xData, status) {

            $.jGrowl.defaults.position = "top-right";
            $.jGrowl.defaults.life = 10000;

            var firstMessage = true;

            $(xData.responseXML).find("z\\:row").each(function () {
                if (firstMessage) {
                    firstMessage = false;
                    $('#two').jGrowl("<div class='ms-vb'><b>You have open tasks.</b><div>",
			            {
			                life: 5000
			            });
                }

                var noteID = $(this).attr("ows_ID");

                var messageHtml =
		                "<div class='ms-vb'>" +
		                "<a href='" + L_Menu_BaseUrl + "/Lists/Tasks/DispForm.aspx?ID=" + $(this).attr("ows_ID")
		                + "&Source=" + window.location + "'>" +
		                "<img src='/_layouts/images/ITTASK.GIF' border='0' align='middle'> " +
		                $(this).attr("ows_Title") + "</a>" +
		                "<br/>" + $(this).attr("ows_Body") +
		                "</div>";

                $('#two').jGrowl(messageHtml, {
                    sticky: true
                });

            }); // end - each function

        } // end - completefunc

    }); // end - SPServices

    // *************************************************************
    // END - Display notifications from 'Tasks' List
    // *************************************************************

}
